﻿using Innoapps.FileTransfer.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Xml;
using System.Xml.Serialization;

namespace Innoapps.FileTransfer.Adapters.Receive
{
    [XmlInclude(typeof(FILE))]
    [XmlInclude(typeof(FTP))]
    [XmlInclude(typeof(SFTP))]
    abstract public class Base
    {
        #region Referential Properties
        [XmlIgnore()]
        public Transfer transfer { get; set; }

        #endregion

        #region Abstract Properties
        [XmlAttribute("uri")]
        abstract public string uri { get; set; }
        #endregion

        #region Properties
        [XmlAttribute("RetryCount")]
        public int RetryCount { get; set; }
        [XmlAttribute("RetryInterval")]
        public int RetryInterval { get; set; }
        [XmlIgnore()]
        public int AttemptCount { get; set; }
        #endregion

        #region Abstract Methods
        abstract public void Receive();
        abstract public void Delete();

        #endregion

        #region Internal Methods

        [MethodImpl(MethodImplOptions.NoInlining)]
        internal void Pass()
        {
            StackTrace st = new StackTrace();
            StackFrame sf = st.GetFrame(1);
            
            ActionResult e = new ActionResult(this.GetType().Namespace + "." + sf.GetMethod().Name, "Complete", "Success");

            transfer.NextAction_Scheduled = DateTime.Now;
            transfer.ActionResults.Add(e);

        }
        [MethodImpl(MethodImplOptions.NoInlining)]
        internal void Fail(string Message)
        {
            StackTrace st = new StackTrace();
            StackFrame sf = st.GetFrame(1);
            ActionResult e = new ActionResult(this.GetType().Namespace + "." + sf.GetMethod().Name, (AttemptCount == RetryCount ? "Failure" : "Error"), Message);
            transfer.NextAction_Scheduled = DateTime.Now.AddSeconds(RetryInterval);
            AttemptCount += 1;
            transfer.ActionResults.Add(e);
        }
        #endregion

    }
}
